/**
 * 拖动甘特图进行移动
 */
export default class HeaderDragMove {
  // 拖动开始位置
  headerDragMoveStart = 0
  // 拖动结束位置
  headerDragMoveEnd = 0
  headerDragZoomMove = null
  headerDragZoomMouseUp = null
  // 拖拽时创建的标识元素
  headerZoomDragElement = null
  // 当前时间轴DOM元素
  headerZoomHeadersElement = null
  headerZoomMove = false
  headerZoomScrollLeft = 0
  // 是否触发timeAxis  刷新甘特图事件
  triggerTimeAxis = true
  hasRenderTimeAxis = false // 当前拖动是否超出时间轴范围（刷新timeAxis）
  // 当前timeAxis 每个单元格时间跨度
  timeTickTime = 0
  onHeaderZoomMouseDown(event) {
    if (event.touches || event.button !== 0) {
      return
    }
    // 拖拽位置位于表头时
    if (
      event.target.nodeName === 'path' ||
      event.target.nodeName === 'rect' ||
      event.target.matches('.b-grid-cell') ||
      event.target.matches('.b-content-element') ||
      event.target.matches('.major-row-title') ||
      event.target.matches('.b-sch-header-timeaxis-cell')
    ) {
      //  b-sch-header-timeaxis-cell
      this.headerDragMoveStart = event.clientX
      this.headerZoomScrollLeft = this.scheduler.scrollLeft
      this.hasRenderTimeAxis = false
      // 当前每个单元格时间跨度
      // 保存当前甘特图时间区间
      const TIMEAXIS = this.scheduler.timeAxis
      this.currentTimeAdjustDate = {
        startDate: TIMEAXIS.adjustedStart,
        endDate: TIMEAXIS.adjustedEnd
      }
      this.timeTickTime = TIMEAXIS.data[0].endDate.getTime() - TIMEAXIS.data[0].startDate.getTime()
      // 保存当前事件（移除使用）
      this.headerDragZoomMove = this.onHeaderZoomMouseMove.bind(this)
      this.headerDragZoomMouseUp = this.onHeaderZoomMouseUp.bind(this)
      document.addEventListener('mousemove', this.headerDragZoomMove, false)
      document.addEventListener('mouseup', this.headerDragZoomMouseUp, false)
      // 保存之前时间轴范围

      // 当前表头dom
      this.headerZoomHeadersElementRect = this.scheduler.subGrids.normal.header.headersElement.getBoundingClientRect()
    }
  }
  /**
   * 拖拽中
   * @param {*} event
   */
  onHeaderZoomMouseMove(event) {
    // 开始拖动
    this.headerZoomMove = true
    this.headerDragMoveEnd = event.pageX
    // 移动滚动轴位置
    this.headerZoomDragEndSetSchedulerDate()
    event.stopPropagation()
    event.preventDefault()
  }
  onHeaderZoomMouseUp(event) {
    // this.headerDragMoveEnd = event.pageX
    // 移动滚动轴位置
    // this.headerZoomDragEndSetSchedulerDate()
    this.removeHeaderDragListener()

    event.stopPropagation()
    event.preventDefault()
  }
  // 拖拽完 设置时间跨度
  headerZoomDragEndSetSchedulerDate() {
    // 移动距离
    const DRAG_INSTANCE = this.headerDragMoveStart - this.headerDragMoveEnd,
      // 计算scrollLeft
      SCROLL_WIDTH = this.headerZoomScrollLeft + DRAG_INSTANCE
    this.setHederZoomScrollToDate(SCROLL_WIDTH, DRAG_INSTANCE)
  }
  /**
   * 移动滚动轴至某个位置或移动至某个日期
   * @param {*} scrollWidth  滚动距离
   * @param {*} moveWidth 移动距离
   */
  setHederZoomScrollToDate(scrollWidth, moveWidth) {
    const SCROLL_WIDTH = this.scheduler.subGrids.normal.header.headersElement.scrollWidth,
      REAL_SCROLL_WIDHT = this.scheduler.subGrids.normal._width + scrollWidth,
      // 当前拖动位置是否超出容器宽度
      OUTOF_RANGE = REAL_SCROLL_WIDHT >= SCROLL_WIDTH
    if (OUTOF_RANGE || scrollWidth < 0) {
      this.setHederZoomScrollShift(moveWidth, SCROLL_WIDTH)
    } else {
      this.scheduler.byRef.virtualScrollers.querySelector('[data-region="normal"]').scrollLeft = scrollWidth
    }
  }
  /**
   * 移动增量
   * @param {*} scrollWidth
   */
  setHederZoomScrollShift(scrollWidth) {
    this.triggerTimeAxis = false
    // 刷新timeAxis
    this.hasRenderTimeAxis = true
    const AMOUNT = 10 // Math.ceil(Math.abs(scrollWidth) / this.scheduler.tickSize)
    // 每次移动一个增量
    // scrollWidth > 0 ? this.scheduler.shiftNext(1) : this.scheduler.shiftPrevious(1);
    // this.scheduler.shift(scrollWidth > 0 ? 1 : -1, this.scheduler.timeAxis.shiftUnit);
    // scrollWidth > 0 && (this.scheduler.virtualScrollers.children[2].scrollLeft = this.scheduler.$canvasWidth)
    // console.log(this.scheduler)
    // 加载事件
    // this.renderEventByEventTypeKey()
    // this.triggerTimeAxis = true;
    let beforeDate = this.scheduler.endDate
    if (scrollWidth > 0) {
      this.scheduler.endDate = new Date(this.scheduler.timeAxis._endMS + AMOUNT * this.timeTickTime)
    } else {
      beforeDate = this.scheduler.startDate
      this.scheduler.startDate = new Date(this.scheduler.timeAxis._startMS - AMOUNT * this.timeTickTime)
    }
    this.scheduler.scrollToDate(beforeDate)
    this.removeHeaderDragListener()
    // let duration = scrollWidth > 0 ? 1 : -1;
    // this.scheduler.startDate = new Date(this.scheduler.timeAxis._startMS + scrollWidth * this.timeTickTime * duration)
    // this.scheduler.endDate = new Date(this.scheduler.timeAxis._endMS + scrollWidth * this.timeTickTime * duration)
  }
  // 移除事件
  removeHeaderDragListener() {
    document.removeEventListener('mousemove', this.headerDragZoomMove, false)
    document.removeEventListener('mouseup', this.headerDragZoomMouseUp, false)
    setTimeout(() => {
      this.headerZoomMove = false
    }, 100)
  }
}
